package setting

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-ini/ini"
	"log"
	"time"
)

type App struct {
	JwtSecret string
	PageSize  int
	PrefixUrl string

	RuntimeRootPath string

	ImageSavePath  string
	ImageMaxSize   int
	ImageAllowExts []string

	ExportSavePath string
	FontSavePath   string

	LogSavePath string
	LogSaveName string
	LogFileExt  string
	TimeFormat  string

	FeatureLedgeSavePath  string
	FeatureLedgeMaxSize   int64
	FeatureLedgeMaxLen    int
	FeatureLedgeAllowExts []string
	KlArchivedSavePath    string
	DESkey                string
	MSMPhone              string
}

var AppSetting = &App{}

type Server struct {
	RunMode          string
	DevServer        bool
	MultiClientLogin bool
	OutputDebug      bool
	DoNotInitMenus   bool
	HttpPort         int
	ReadTimeout      time.Duration
	WriteTimeout     time.Duration
}

var ServerSetting = &Server{}

type Database struct {
	Type        string
	User        string
	Host        string
	Port        int
	Name        string
	TablePrefix string
	LogMode     bool
}

var DatabaseSetting = &Database{}

type Redis struct {
	Host          string
	User          string
	Password      string
	DbName        int
	MaxIdle       int
	MaxActive     int
	MaxActiveWait bool
	IdleTimeout   time.Duration
}

var RedisSetting = &Redis{}

type Aliyun struct {
	OssEndpoint         string
	OssEndpointInternal string
	OssBucketLicence    string
	OssLicenceAck       string
	AccessKeyID         string
	AccessKeySecret     string
	SMSSign             string
}

var AliyunSetting = &Aliyun{}

var StaticsSetting = &Statics{}

type Statics struct {
	StaticPath                string
	FeatureLegendFile         string
	SectionListCode           string
	SectionFeatureUnusualCode string
	SyndromeClassListCode     string
	SyndromeFeatureOptFile    string
	KnowledgeVersionList      string
}

var ArmdSetting = &Armd{}

type Armd struct {
	AccessKeyId     string
	AccessKeySecret string
	TargetUrl       string
}

var cfg *ini.File
var RunMode string

func InitSetUp() {
	var err error
	// 设置环境类型方式
	// 1、配置文件形式控制环境类型
	cfg, err = ini.Load("conf/app.ini")
	if err != nil {
		log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
	}
	RunMode = cfg.Section("").Key("RunMode").String()
	// 2、直接设置环境类型
	//gin.SetMode(gin.ReleaseMode)
	gin.SetMode(RunMode) // 设置模式，有三种模式：debug release test
	fmt.Println("已开启模式：", RunMode)
	cfg, err = ini.Load(fmt.Sprintf("conf/app_%s.ini", gin.Mode()))
	if err != nil {
		log.Fatalf("setting.Setup, fail to parse 'conf/app_%s.ini': %v", gin.Mode(), err)
	}
}

func Setup() {
	InitSetUp()

	mapTo("app", AppSetting)
	mapTo("server", ServerSetting)
	mapTo("database", DatabaseSetting)
	mapTo("redis", RedisSetting)
	mapTo("statics", StaticsSetting)
	mapTo("aliyun", AliyunSetting)
	mapTo("armd", ArmdSetting)

	AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
	ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
	ServerSetting.WriteTimeout = ServerSetting.ReadTimeout * time.Second
	AliyunSetting.AccessKeyID = "LTAIqCCjg4m8cQMS"
	AliyunSetting.AccessKeySecret = "SzQByRoAYU4yDvSXIqo6Y5PJtnIels"
	//fmt.Println(fmt.Sprintf("数据库配置：%v", DatabaseSetting))
	//fmt.Println(fmt.Sprintf("数据库配置：%v", AliyunSetting))
	//fmt.Println(fmt.Sprintf("数据库配置：%v", cfg))
}

// mapTo map section
func mapTo(section string, v interface{}) {
	err := cfg.Section(section).MapTo(v)
	if err != nil {
		log.Fatalf("Cfg.MapTo RedisSetting err: %v", err)
	}
}
